package com.bolt.support.codegenerator.model;

import com.bolt.common.utils.StrUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据表模型类
 *
 * @author　michao Created by Administrator on 2018/2/2.
 */
public class Table {
    //表名
    private String sqlName;

    private String className;
    //略注
    private String remarks;

    private String tableType;

    private String tableAlias;

    private String catalog = null;

    private String schema = null;

    private List<Column> columns = new ArrayList<>();


    private Column pkColumn;

    public String getSqlName() {
        return sqlName;
    }

    public void setSqlName(String sqlName) {
        this.sqlName = sqlName;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getTableType() {
        return tableType;
    }

    public void setTableType(String tableType) {
        this.tableType = tableType;
    }

    public String getTableAlias() {
        return tableAlias;
    }

    public void setTableAlias(String tableAlias) {
        this.tableAlias = tableAlias;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public Column getColumn(String columnName) {

        for (Column column : columns) {
            if (column.getSqlName().equals(columnName)) {
                return column;
            }
        }
        return null;
    }

    public void addColumn(Column column) {
        this.columns.add(column);
    }

    public String getClassName(boolean full) {
        String defaultValue = getSqlName();
        if (full) {
            defaultValue = StrUtil.toCamelCase(defaultValue);
        } else {
            if (StrUtil.indexOf(defaultValue, '_') > 0) {
                defaultValue = StrUtil.subAfter(defaultValue, "_", false);
                defaultValue = StrUtil.toCamelCase(defaultValue);
            }
        }
        return StrUtil.emptyIf(className, StrUtil.upperFirst(defaultValue));
    }

    public void initClassName(boolean full) {
        this.className = getClassName(full);
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public Column getPkColumn() {
        return pkColumn;
    }

    public void setPkColumn(Column pkColumn) {
        this.pkColumn = pkColumn;
    }
}
